package com.kaitian.demo.demo_boot_starter.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author ：xietian
 * @version ：V1.0
 * @program ：demo
 * @date ：Created in 2022/4/7 10:03
 * @description ：Bean生命周期测试
 */
@Slf4j
@Component
public class BeanServiceImpl implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {

    @Override
    public void setBeanName(String s) {
        log.info("1.【{}】调用BeanNameAware的setBeanName: {}", this.getClass().getSimpleName(), s);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.info("2.【{}】调用BeanFactoryAware的setBeanFactory: {}", this.getClass().getSimpleName(), beanFactory);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("3.【{}】调用ApplicationContextAware的setApplicationContext: {}", this.getClass().getSimpleName(), applicationContext);
    }

    @PostConstruct
    public void init() {
        log.info("4.【{}】调用注解@PostConstruct定义的自定义初始化方法", this.getClass().getSimpleName());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("5.【{}】调用InitializingBean的afterPropertiesSet", this.getClass().getSimpleName());
    }

    @PreDestroy
    public void preDestory() {
        log.info("6.【{}】调用注解@PreDestory定义的自定义销毁方法", this.getClass().getSimpleName());
    }

    @Override
    public void destroy() throws Exception {
        log.info("7.【{}】调用DisposableBean的destroy", this.getClass().getSimpleName());
    }

}